<?php
/**
 * 邮储食堂正式会员申请
 * User: cxm
 * Date: 2020/12/4
 * Time: 9:30
 */
namespace App\Http\Controllers\Admin\Member;
use App\Http\Controllers\Admin\Controller;
use App\Models\Advise;
use App\Models\RegularUserApply;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserApplyController extends  Controller {
    /**
     * 正式会员申请列表
     * URL POST:/admin/member/apply/getLists
     * @param Request $request
     * @param string start_time 开始时间
     * @param string end_time 结束时间
     * @param int type 1用户手机 2用户名
     * @param string keyword 关键字
     * @param int is_card 是否办理工会卡 1是 2否
     * @param int is_food 是否食堂卡 1是 2否
     * @param int page 页码
     * @param int limit 每页显示条数
     * @return mixed
     */
    public function getLists(Request $request){
        $params = $request->only(['type','start_time','end_time','keyword','is_card','is_food','page','limit']);
        // 关键字
        $type = isset($params['type']) ? $params['type'] : 1;
        $keyword = isset($params['keyword']) ? $params['keyword'] : '';
        if (!empty($keyword)) {
            switch($type) {
                case 1: // 用户手机
                    $params['where'][] = ['mobile','like','%'.$keyword.'%'];
                    break;
                case 2:// 用户名
                    $params['where'][] = ['name','like','%'.$keyword.'%'];
                    break;
            }
        }
        // 是否办理工会卡
        if (isset($params['is_card']) && $params['is_card'] == true) {
            $params['where'][] = ['is_card',1];
        }
        // 是否办理食堂卡
        if (isset($params['is_food']) && $params['is_food'] == true) {
            $params['where'][] = ['is_food',1];
        }
        // 投诉时间筛选
        $start_time = isset($params['start_time']) ? $params['start_time'] : '';
        if (!empty($start_time)) {
            $params['where'][] = ['created_at','>=',$start_time];
        }
        $end_time = isset($params['end_time']) ? $params['end_time'] : '';
        if (!empty($end_time)) {
            $params['where'][] = ['created_at','<=',$params];
        }
        $result = RegularUserApply::listByWhere($params);
        return $this->success('成功',$result);
    }

    /**
     * 处理
     * URL POST:/admin/member/apply/dispose
     * @param Request $request
     * @param int id 申请ID
     * @param string dispose_remark 处理备注
     * @return mixed
     */
    public function dispose(Request $request){
        $params = $request->only(['id','remark']);
        if (!array_key_exists('id',$params) || empty($params['id'])) {
            return $this->error('投诉ID是必须的');
        }
        $advise = RegularUserApply::find($params['id']);
        if (!$advise) {
            return $this->error('申请不存在');
        }
        if ($advise->status == 1) {
            return $this->error('申请已处理');
        }
        $update_arr = array(
            'status'=>1,
            'dispose_remark'=>$params['remark'],
            'dispose_time'=>Carbon::now()
        );
        $result = RegularUserApply::modify(array('id'=>$params['id']),$update_arr);
        if ($result) {
            return $this->success('成功');
        }
        return $this->error('失败');
    }

    /**
     * 删除
     * URL POST:/admin/member/apply/delete
     * @param Request $request
     * @param array ids 申请ID 数组
     * @return mixed
     */
    public function delete(Request $request){
        $params = $request->only(['ids']);
        if (!array_key_exists('ids',$params) || empty($params['ids'])) {
            return $this->error('缺少投诉ID');
        }
        $result = RegularUserApply::whereIn('id',$params['ids'])->delete();
        if ($result) {
            return $this->success('成功');
        }
        return $this->error('失败');
    }
}